package com.owc.common.encryption;

import com.owc.common.converter.IDecodeAdvice;
import com.owc.common.exception.DecodeException;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Optional;

public class Base64Decode implements IDecodeAdvice<String> {
    @Override
    public String decode(String encryption) {
        return Optional.ofNullable(encryption)
                .map(ostr -> URLDecoder.decode(new String(Base64.getDecoder().decode(encryption.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8)))
                .orElseThrow(DecodeException::new);
    }
}
